package net.lizhongxian.interview.hunktimes.entity;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 超市类
 */
public class Shop {

	/**
	 * 商品列表
	 */
	private final List<Goods> goodsList;

	public Shop() {
		this.goodsList = new ArrayList<>();
	}

	/**
	 * 添加商品
	 *
	 * @param goods 商品
	 */
	public void add(Goods goods) {
		this.goodsList.add(goods);
	}

	/**
	 * 是否存在此商品
	 *
	 * @param goods 商品
	 * @return true-是 false-否
	 */
	public boolean contains(Goods goods) {
		return this.goodsList.contains(goods);
	}

	/**
	 * 创建新订单
	 *
	 * @return 订单
	 */
	public Order newOrder() {
		return new Order(this);
	}

	/**
	 * 结账
	 *
	 * @param order 订单
	 */
	public void check(Order order) {
		System.out.println("开始进行结账……");
		Map<Goods, BigDecimal> goodsList = order.getGoodsList();
		List<Coupon> couponList = order.getCouponList();
		BigDecimal sum = new BigDecimal(0); // 订单总价
		BigDecimal result = new BigDecimal(0);

		// 处理商品列表，计算总价
		System.out.println("商品名称\t商品数量\t商品单价\t折扣");
		for (Goods goods : goodsList.keySet()) {
			System.out.println(goods.getName() + "\t" + goodsList.get(goods) + "\t" + goods.getPrice() + "\t" + goods.getDiscount());
			// 订单总价 = 商品价格 * 商品数量 * 商品折扣
			sum = sum.add(goods.getPrice().multiply(goodsList.get(goods)).multiply(goods.getDiscount()));
		}
		System.out.println("==========");
		System.out.println("总价：" + sum);
		result = result.add(sum);

		// 处理优惠券
		for (Coupon coupon : couponList) {
			// TODO 优惠券与商品折扣可否叠加？此处以可叠加计算
			if (sum.compareTo(coupon.getDemand()) >= 0) {
				System.out.println("使用优惠券 满" + coupon.getDemand() + "减" + coupon.getReduction());
				result = result.subtract(coupon.getReduction());
			} else {
				System.out.println("优惠券 满" + coupon.getDemand() + "减" + coupon.getReduction() + " 不满足条件");
			}
		}
		System.out.println("需支付金额：" + result);
		System.out.println();
	}

}
